package com.ark.cloud.dataservices.modules.sys.service.impl;

import com.ark.cloud.dataservices.common.exception.SeatNotFoundException;
import com.ark.cloud.dataservices.modules.sys.dao.ModuleMapper;
import com.ark.cloud.dataservices.modules.sys.entity.po.Module;
import com.ark.cloud.dataservices.modules.sys.service.IModuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;

@Service
@Slf4j
public class ModuleService extends ServiceImpl<ModuleMapper, Module> implements IModuleService {

    @Override
    public boolean add(Module module) {
        return this.save(module);
    }

    @Override
    public boolean delete(String id) {
        //删除 seat_module_relation 表中的数据
        this.getBaseMapper().deleteSMRByModuleId(id);
        return this.removeById(id);
    }

    @Override
    public Set<String> getSeatNamesByModuleId(String id) {
        return this.getBaseMapper().getSeatNamesByModuleId(id);
    }

    @Override
    public List<Module> all() {
        List<Module> list = this.list();
        list.forEach(m -> m.setSeatNames(this.getSeatNamesByModuleId(m.getId())));
        return list;
    }

    @Override
    public boolean addSeatsToModuleBySeatIds(String id, Set<String> seatIds) {
        this.getBaseMapper().deleteSMRByModuleId(id);
        seatIds.forEach(seatId -> this.getBaseMapper().insertToSMR(id, seatId));
        return true;
    }

    @Override
    public Module get(String id) {
        Module module = this.getById(id);
        if (Objects.isNull(module)) {
            throw new SeatNotFoundException("module not found with id:" + id);
        }
        return module;
    }

    @Override
    public boolean update(Module module) {
        return this.updateById(module);
    }
}
